package link.anyauto.j2m;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by LYQ on 2016/5/6.
 */
public class ProviderAct extends Activity {

    @Bind(R.id.et_desc)
    EditText etDesc;
    @Bind(R.id.et_tel)
    EditText etTel;
    @Bind(R.id.et_user_comp_id)
    EditText etUserCompId;
    @Bind(R.id.btn_save_user_info)
    Button btnSaveUserInfo;
    @Bind(R.id.et_comp_id)
    EditText etCompId;
    @Bind(R.id.et_comp_business)
    EditText etCompBusiness;
    @Bind(R.id.et_comp_addr)
    EditText etCompAddr;
    @Bind(R.id.btn_save_comp_info)
    Button btnSaveCompInfo;
    @Bind(R.id.et_search_tel)
    EditText etSearchTel;
    @Bind(R.id.btn_search)
    Button btnSearch;
    @Bind(R.id.btn_del)
    Button btnDel;
    @Bind(R.id.tv_result)
    TextView tvResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_provider);
        ButterKnife.bind(this);
    }

    @OnClick(R.id.btn_save_user_info)
    public void saveUsers(View view) {
        saveUserInfo();
        view.postDelayed(new Runnable() {
            @Override
            public void run() {
                queryPostCode();
            }
        }, 1000);
    }

    @OnClick(R.id.btn_save_comp_info)
    public void saveCompany(View view) {
        saveCompanyInfo();
    }

    @OnClick(R.id.btn_del)
    public void del(View view) {
        getContentResolver().delete(Uri.parse("content://" + AUTHORITY + "/" + TABLE_USER_INFO), "tel_num = ?", new String[]{etSearchTel.getText().toString()});
    }

    @OnClick(R.id.btn_search)
    public void search(View view) {
        Uri query = Uri.parse("content://" + AUTHORITY + "/userinfo/" + etSearchTel.getText().toString());
        Cursor cursor = getContentResolver().query(query, null, null, null, null);
        String msg = "";
        while (cursor.moveToNext()) {
            msg += "\n个人信息：" + cursor.getString(cursor.getColumnIndex(DESC))
                    + "\n电话：" + cursor.getString(cursor.getColumnIndex(TEL))
                    + "\n公司编号：" + cursor.getString(cursor.getColumnIndex(COMP_ID));
        }
        tvResult.setText(msg);
    }

    void saveUserInfo() {
        ContentValues values = new ContentValues();
        values.put(DESC, etDesc.getText().toString());
        values.put(COMP_ID, etUserCompId.getText().toString());
        values.put(TEL, etTel.getText().toString());
        getContentResolver().insert(Uri.parse("content://" + AUTHORITY + "/" + TABLE_USER_INFO), values);
    }

    void saveCompanyInfo() {
        ContentValues values = new ContentValues();
        values.put(ID, etCompId.getText().toString());
        values.put(BUSINESS, etCompBusiness.getText().toString());
        values.put(ADDR, etCompAddr.getText().toString());
        getContentResolver().insert(Uri.parse("content://" + AUTHORITY + "/" + TABLE_COMPANY), values);
    }

    void queryPostCode() {
        Uri query = Uri.parse("content://" + AUTHORITY + "/userinfo/123456");
        Cursor cursor = getContentResolver().query(query, null, null, null, null);
        if (cursor.moveToFirst()) {
            Toast.makeText(this, "电话来自：" + cursor.getString(2), Toast.LENGTH_LONG).show();
        }
    }

    static final String AUTHORITY = "link.anyauto.provider";

    public static final String TABLE_USER_INFO = "userinfo";
    public static final String TABLE_COMPANY = "company";

    public static final String TEL = "tel_num";
    public static final String DESC = "desc";
    public static final String COMP_ID = "comp_id";
    public static final String ID = "id";
    public static final String BUSINESS = "business";
    public static final String ADDR = "addr";
}
